package com.octnote.controller;

import com.octnote.domain.contacts.Contacts;
import com.octnote.message.Message;
import com.octnote.service.ContactsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理联系人类接口
 * Created by leohehe on 2017/4/13.
 */
@RestController
@PropertySource("classpath:message.properties")
@RequestMapping(value = "/api/v1/", name = "联系人API")
public class ContactsController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ContactsController.class);
    private Message message = new Message();
    private ContactsService contactsService;

    @Autowired
    public ContactsController(ContactsService contactsService){
        this.contactsService = contactsService;
    }

    /**
     * 获取联系人列表-不分页
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "contacts/getContacts", method = RequestMethod.GET)
    public ResponseEntity<Message> getContacts(@RequestParam(required = false) String searchPhrase,Pageable p){
        Page<Contacts> contacts = contactsService.getContacts(searchPhrase, p);
        message.setMsg(1, "获取联系人列表成功", contacts);
        return new ResponseEntity<Message>(message, HttpStatus.OK);
    }

    /**
     * 同步联系人列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "contacts/sycnContacts", method = RequestMethod.POST)
    public ResponseEntity<Message> snycContacts(@RequestBody List<Contacts> contactsList){
        List<Contacts>  syncContactsList = contactsService.syncContacts(contactsList);
        message.setMsg(1, "同步联系人列表成功", syncContactsList);
        return new ResponseEntity<Message>(message, HttpStatus.OK);
    }
}
